<?php

namespace Web\MainAppBundle\Controller;


use Model\MessageBundle\Entity\Message;
use Model\MessageBundle\Model\MessageModel;
use Model\UserBundle\Model\UserLoginLogModel;
use Model\UserBundle\Model\UserModel;
use Model\UserBundle\Setting\UserSetting;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Security;

class UserController extends AppBaseController
{
    /**
     * @route("preConnect",name="preConnect")
     */
    public function preConnect()
    {
        return new Response("1.0.0.0",200);
    }

    /**
     * @route("user/login/log",name="AppUserLoginLog")
     */
    public function AppUserLoginLog(Request $request){
        $userLoginLog = new UserLoginLogModel($this->getDoctrine());
        if ($user = $this->getUser()) {
            $logger = $this->get("logger");
            $logger->info(json_encode($user));
            $loginInfo = $userLoginLog->createLoginLogInfo($user);
            $userLoginLog->addLoginLog($loginInfo);
            $data = array(
                "userId"    => $user->getId(),
                "name"    => $user->getNickName() ? $user->getNickName() : $user->getAccount(),
                "headPic"    => $user->getHeadPic(),
            );
            $userModel = New UserModel($this->getDoctrine());
            $userInfo = $userModel->getUserInfo($user->getId());
            return $this->json_out(1,"信息获取成功",$userInfo);
        } else {
            $logger = $this->get("logger");
            $logger->info("--------------");
            $logger->info(json_encode($user));
            $loginInfo = $userLoginLog->createLoginLogInfo();
            $userLoginLog->addLoginLog($loginInfo);

            return $this->json_out(0,"账号或密码错误");
        }

    }

    /**
     * @route("user/info",name="AppUserInfo")
     */
    public function AppUserInfo(Request $request)
    {
        $user = $this->needToBeUser();
        $userModel = New UserModel($this->getDoctrine());
        $uid = $user->getId();
        if ($request->getMethod("POST") == "POST")
        {//更新账号信息
            if (!$nickName = $this->getParamRequired("nickName")) {return $this->json_out(0,"请输入昵称");}
            if (!$email = $this->getParamRequired("email")) {return $this->json_out(0,"请输入email");}
            if (!$phone = $this->getParamRequired("phone")) {return $this->json_out(0,"请输入手机号");}
            $data = array();
            $data["nickName"] = $nickName;
            $data["email"] = $email;
            $data["phone"] = $phone;
            $userModel->updateUserInfo($uid,$data);

            $userInfo = $userModel->getUserInfo($uid);
            $data = $userInfo;
            return $this->json_out(1,"信息更新成功",$data);
        } else {

            $userInfo = $userModel->getUserInfo($uid);
            $data = $userInfo;
            return $this->json_out(1,"信息获取成功",$data);
        }
    }


    /**
     * @route("user/password",name="AppUserPassword")
     */
    public function AppUserPasswordAction(Request $request)
    {
        $user = $this->needToBeUser();
        $userModel = New UserModel($this->getDoctrine());
        $uid = $user->getId();

        if (!$oldPassword = $this->getParamRequired("oldPassword")) {return $this->json_out(0,"请输入原密码");}
        if ($user->getPassword() != md5($oldPassword)) {return  $this->json_out(0,"请输入正确的原密码");}
        if (!$newPassword = $this->getParamRequired("newPassword")) {return $this->json_out(0,"请输入新密码");}
        if (strlen($newPassword) < 6) {return $this->json_out(0,"请设置6位以上新密码");}
        $newPasswordAgain = $this->getParamRequired("newPasswordAgain");
        if ($newPasswordAgain !== $newPassword ) {return $this->json_out(0,"两次输入的新密码不一致");}
        $data = array();
        $data["password"] = md5($newPassword);
        $userModel->updateUserInfo($uid,$data);

        return $this->json_out(1,"修改成功");
    }


    /**
     * @route("user/headPic",name="AppUserHeadPic")
     */
    public function AppUserHeadPicAction(Request $request)
    {
        $user = $this->needToBeUser();
        $userModel = New UserModel($this->getDoctrine());
        $uid = $user->getId();
        if (!$headPic = $this->getParamRequired("headPic")) {return $this->json_out(0,"您似乎没有选择头像啊？！");}

        $data = array();
        $data["headPic"] = $headPic;
        $userModel->updateUserInfo($uid,$data);
        $userInfo = $userModel->getUserInfo($uid);
        return $this->json_out(1,"修改成功",$userInfo);
    }

    /**
     * @route("user/messages",name="AppUserMessages")
     */
    public function AppUserMessagesAction()
    {
        $user = $this->needToBeUser();
        $data = array("user" => $user);
        $messageModel = new MessageModel($this->getDoctrine());
        $messages = $messageModel->getListForUser($user->getId());
        $data["messages"] = $messages;
        return $this->json_out(1,"修改成功",$data);
    }

    /**
     * @route("user/message/{id}",name="AppUserMessageShow")
     */
    public function AppUserMessageShowAction(Message $message)
    {
        $user = $this->needToBeUser();
        $data["message"] = $message;
        return $this->render("WebMainSiteBundle:User:user_message_show.html.twig",$data);
    }
}
